package com.ag.oa.core.service.impl;

import com.ag.oa.core.dao.IMessageDao;
import com.ag.oa.core.dao.IProjectDao;
import com.ag.oa.core.dao.IUserDao;
import com.ag.oa.core.entity.vo.MessageVO;
import com.ag.oa.core.entity.vo.UserVO;
import com.ag.oa.core.service.IMessageService;
import com.ag.oa.util.CustomUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("messageServiceImpl")
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private IMessageDao messageDao;

    @Autowired
    private IUserDao userDao;

    @Autowired
    private IProjectDao projectDao;

    @Override
    public void getUserUnreadMessageCount(String openId, JSONObject res) {

        int total = 0;
        int code = 201;

        UserVO user = userDao.getById(openId);
        if (user!=null) {
            total = messageDao.getUnreadMessageCount(user.getId());

            code = 200;
            res.put("userId", user.getId());
        }
        res.put("resCode", code);
        res.put("unreadCount", total);
    }

    @Override
    public List<Map> getUserAllMessagesData(Integer userId, Integer isRead, Integer page, Integer pageSize) {

        if (page==null) {
            page = 0;
        }
        if (pageSize==0) {
            pageSize = 15;
        }
        return messageDao.getUserMessagesData(userId, isRead, page * pageSize, pageSize);
    }

    /**
     * 保存会员申请消息记录
     * @param type 1: 会员申请
     */
    @Override
    public void saveNewOneMessageRecord(Integer type, Integer fromUserId, List<Integer> toUserIdList
            , Integer taskId, String remark, Integer completeScore) {

        String batchId = CustomUtil.getUUID32();
        if (type==1) {
            // 决策用户就是接收人
            List<Integer> arrayList = userDao.getAllDecisionUserPrimaryKeys();
            messageDao.saveNewMessage(type, fromUserId, arrayList, taskId, remark, completeScore
                    , batchId, null, 0, null);
        }
    }

    @Override
    public Map<String, Object> getMessageInfoById(Integer id) {

        messageDao.updateToRead(id);
        return messageDao.getInfoById(id);
    }

    @Override
    public void updateHandleMembershipApplyMessage(Integer id, Integer userId, Integer status) {

        // 同意用户的 vip 申请
        if (status==1) {
            userDao.updateVipById(userId, 1);
        }
        // 同意或忽略 修改处理状态
        messageDao.updateHandleStatusById(id, status);
    }

    @Override
    public void getUserTaskForumMessages(Integer taskId, Integer userId, JSONObject res) {

        List<Map> forumMessages = messageDao.getUserTaskAllForumMessagesData(taskId);
        res.put("forumMessages", forumMessages);
    }

    @Override
    public void updateMessageToRead(Integer id) {

        messageDao.updateToRead(id);
    }

    @Override
    public Integer updateAgreedCompleteTask(MessageVO vo) {

        Integer selectCount = messageDao.updateTaskUserCompleteScore(vo.getCompleteScore()
                , vo.getUpdatedBy(), vo.getTaskId(), vo.getUserId(), vo.getTaskType()
                , vo.getTaskScore());

        // 任务用户表的，奖励积分修改成功后，才修改消息表的数据
        if (selectCount==1) {
            messageDao.updateMessageCompleteScore(vo.getCompleteScore(), vo.getMessageId());
        }
        return selectCount;
    }

    @Override
    public UserVO getMessageLastUpdateManagerInfo(Integer messageId) {

        return messageDao.getMessageLastUpdateManagerInfo(messageId);
    }

    @Override
    public void updateProjectInvitedData(Integer id, Integer projectId, Integer userId, Integer status) {

        messageDao.updateHandleStatusById(id, status);

        if (status==1) {  // 同意合作社成员邀请
            projectDao.saveMemberUser(projectId, userId, userId, 0); // 创建人也是自己
        }
    }

}
